//
//  GPMapController.m
//  GoldPig
//
//  Created by 虎爷 on 2020/1/3.
//  Copyright © 2020 虎爷. All rights reserved.
//

#import "GPMapController.h"
#import "GPMapCell.h"
#import "GPMapHeadView.h"
#define UIHexColor(name) [UIColor colorFromHexString:name]
@interface GPMapController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *levelArr;
@end

@implementation GPMapController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIHexColor(@"#43D7CC");
}

- (void)setLevel:(NSNumber *)level{
    _level = level;
    [self getPigInfoListResquest];
}

-(void)setupUI{
    [self tableView];

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:

    [self.tableView numberOfRowsInSection:0]-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
    [self setupHead];

}

-(void)setupHead{
    GPMapHeadView *headView = [GPMapHeadView initGPMapHeadView];
    headView.frame = CGRectMake(0, KSafeStateHeight, kScreenWidth, 110);
    headView.levelLab.text = [NSString stringWithFormat:@"已升到%@级",self.level];
    [self.view addSubview:headView];
    WEAKSELF
    headView.closeCilckBlock = ^{
        [weakSelf.navigationController popViewControllerAnimated:NO];
    };
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -KSafeAreaTopHeight, kScreenWidth, kScreenHeight-KSafeAreaBottonHeight+KSafeStateHeight+KSafeAreaTopHeight)
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        
        UIImageView * foot = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 110)];
        foot.image = [UIImage imageNamed:@"地图底部"];
        _tableView.tableFooterView = foot;
        
        
        [self.view addSubview:_tableView];
    }
    return _tableView;
    
}


#pragma mark TableViewDelegate&DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GPMapCell *cell = [GPMapCell cellWithTableView:tableView];
    cell.levelArr = self.levelArr;
    cell.level = self.level;
    cell.row = indexPath.row;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 440;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

-(void)getPigInfoListResquest{
    
    NSDictionary *dic = [[NSDictionary alloc]init];
    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(getPigInfoList) params:dic  success:^(NSInteger code, id  _Nonnull result) {

        if (code == 1) {
            NSLog(@"%@",result);
            self.levelArr = result[@"data"];
            
            [self setupUI];
        }

    } fail:^(NSError * _Nonnull error) {

    }];
}


@end
